package com.Lab427.workflow.service.DAGExecutor;

import com.Lab427.workflow.common.enums.ParamsType;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static com.Lab427.workflow.common.constants.SystemConstant.CONNECT;

/**
 * @author Yue
 * @date:2025/8/26 21:09
 * 根据节点类型和参数，决定做那个任务
 */

@RequiredArgsConstructor
@Service
public class NodeService {


    private final ChatClient chatClient;
    // 输入 word 数据
    public String wordInput(Map<String, Object> params) {
        // TODO: 处理输入
        return "处理后的 word 数据";
    }
    // 输出 word 数据
    public String wordOutput(Map params) {
        // TODO: 输出或存储
        return "输出结果";
    }

    // 调用大模型处理文本
    public String analyseData(Map params) {
        textOutput(params);
        return "模型输出结果";
    }

    // 文本输出
    public String textOutput(Map<String, Object> params) {
        // TODO: 输出或存储
        String input = params.get(ParamsType.TEXT_INPUT.getValue()).toString();
        Map<String, Object> outputDataMap = (Map<String, Object>) params.get(ParamsType.OUTPUT_LASTBATCH.getValue());
        StringBuffer sb = new StringBuffer();
        sb.append("");
        //拼接config多个结果
        if (outputDataMap!=null){
            for (String key : outputDataMap.keySet()) {
                String str = outputDataMap.get(key).toString();
                sb.append(str);
            }
        }
        sb.append(CONNECT).append(input);
        String prompt = sb.toString();
        //调用大模型
        String res = chatClient.prompt()
                .user(prompt)
                .call()
                .content();
        System.out.println(res);
        return res;
    }
}
